﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;
using System.Data;
using System.Configuration;
using Comm;
using System.Text;

namespace MedicareNoteManagerWeb.Web
{
    public partial class Return : System.Web.UI.Page
    {
        string path = ConfigurationManager.ConnectionStrings["routes"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //设定初始排序字段为文件创建时间
                grv_Return.Attributes.Add("SortExpression", "创建时间");
                grv_Return.Attributes.Add("SortDirection", "DESC");
                this.BindGrid();
            }
        }
        // 获取指定目录下的文件        
        public static DataTable GetFils(string Path)
        {
            DataTable dt = new DataTable();
            DataColumn dc;
            DataRow dr;

            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.String");
            dc.ColumnName = "文件名";
            dt.Columns.Add(dc);

            //dc = new DataColumn();
            //dc.DataType = System.Type.GetType("System.String");
            //dc.ColumnName = "文件格式";
            //dt.Columns.Add(dc);

            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.DateTime");
            dc.ColumnName = "创建时间";
            dt.Columns.Add(dc);
            //dc = new DataColumn();
            //dc.DataType = System.Type.GetType("System.DateTime");
            //dc.ColumnName = "修改时间";
            //dt.Columns.Add(dc);

            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.String");
            dc.ColumnName = "所属目录";
            dt.Columns.Add(dc);

            //dc = new DataColumn();
            //dc.DataType = System.Type.GetType("System.String");
            //dc.ColumnName = "文件大小";
            //dt.Columns.Add(dc);

            if (!System.IO.Directory.Exists(Path))
            {
                throw new Exception("不存在相应目录");
            }
            else
            {
                DirectoryInfo thisOne = new DirectoryInfo(Path);                
                FileInfo[] fileInfo = thisOne.GetFiles();
                foreach (FileInfo fInfo in fileInfo)
                {
                    dr = dt.NewRow();
                    dr["文件名"] = fInfo.Name;
                    //dr["文件格式"] = fInfo.Extension;
                    dr["创建时间"] = fInfo.CreationTime;
                    //dr["修改时间"] = fInfo.LastWriteTime;
                    dr["所属目录"] = fInfo.DirectoryName;
                    //dr["文件大小"] = fInfo.Length.ToString();
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        //操作
        protected void grv_Return_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")//删除备份
            {
                GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); //获取被点击的linkButton所在的GridViewRow
                int indexr = gvrow.RowIndex; //获取到行索引
                string fielname=path+"//"+this.grv_Return.Rows[indexr].Cells[0].Text;
                if(Directory.Exists(path))
                {
                    if(File.Exists(fielname))
                        File.Delete(fielname);
                    MessageBox.ShowDialog(this.Page, "删除成功", "success");
                }
                Response.Redirect("Return.aspx");
            }
            if (e.CommandName == "return")//还原备份
            {
                string DBName = ConfigurationManager.ConnectionStrings["DBName"].ToString();
                string DBUserName = ConfigurationManager.ConnectionStrings["DBUserName"].ToString();
                string DBPwd = ConfigurationManager.ConnectionStrings["DBPwd"].ToString();
                string ServerIp = ConfigurationManager.ConnectionStrings["ServerIp"].ToString();
                string ServerPort = ConfigurationManager.ConnectionStrings["ServerPort"].ToString();
                string character = ConfigurationManager.ConnectionStrings["character"].ToString();
                GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); //获取被点击的linkButton所在的GridViewRow
                int indexr = gvrow.RowIndex; //获取到行索引
                string fielname = path + "//" + this.grv_Return.Rows[indexr].Cells[0].Text;
               try
               {
                //构建执行的命令
                StringBuilder sbcommand = new StringBuilder();
                StringBuilder sbfileName = new StringBuilder();
                String command = string.Format("mysql --quick --host={0} --default-character-set={1} --lock-all-tables --port={2} --user={3} --password={4} --databases {5} <" + fielname, ServerIp, character, ServerPort, DBUserName, DBPwd, DBName);
                //获取mysql.exe所在路径
                String appDirecroty = ConfigurationManager.ConnectionStrings["mysqldump"].ToString(); 
                Cmd.StartCmd(appDirecroty, command);
                MessageBox.ShowDialog(this.Page, "还原成功", "success");
              }
              catch 
                {
                    MessageBox.ShowDialog(this.Page, "还原失败", "success");
                }
            }            
            }
        //分页
        protected void grv_Return_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //this.grv_Return.PageIndex = e.NewPageIndex;
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了Go按钮
                TextBox txtNewPageIndex = null;

                //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    //得到text控件
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }
            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
            //得到新的值
            theGrid.PageIndex = newPageIndex;
            //重新绑定
            this.BindGrid();
        }
        //排序
        protected void grv_Return_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression.ToString();
            string sortdirection = "ASC";
            if (sortExpression == grv_Return.Attributes["SortExpression"])         
            {
                sortdirection = (grv_Return.Attributes["SortDirection"].ToString() == sortdirection ? "DESC" : "ASC");
            }
            grv_Return.Attributes["SortExpression"] = sortExpression;
            grv_Return.Attributes["SortDirection"] = sortdirection;
            this.BindGrid();
        }
        //绑定数据
        private void BindGrid()
        {
            //获取数据源
            DataTable dtb =GetFils(path);
            if (dtb.Rows.Count==0) 
            {
                this.divrt.Style["display"] = "block";
            }
            else 
            {
                this.divrt.Style["display"] = "none";
            //排序
            string SortDirection = grv_Return.Attributes["SortDirection"].ToString();
            string SortExpression = grv_Return.Attributes["SortExpression"].ToString();
            dtb.DefaultView.Sort = string.Format("{0} {1}", SortExpression, SortDirection);
            //赋数据源并绑定
            grv_Return.DataSource = dtb;
            grv_Return.DataBind();
            }
        }



     }
}
